﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solidi
{
	/// <summary>
	/// Thread safe
	/// </summary>
	sealed class ContextualDependencyComposer : IDependencyComposer
	{
		private object _graphId;
		private IDependencyComposer _composer;

		public ContextualDependencyComposer(object graphId, IDependencyComposer composer)
		{
			_graphId = graphId;
			_composer = composer;
		}

		public bool TryCompose(object graphId, IDependencyContainer container, Type type, IEnumerable<IDependencyParameter> arguments, out object instance)
		{
			if (_graphId != graphId)
			{
				throw new Exception("Called from root");
			}

			return _composer.TryCompose(graphId, container, type, arguments, out instance);
		}

		public bool TryCompose(IDependencyContainer container, Type type, IEnumerable<IDependencyParameter> arguments, out object instance)
		{
			return _composer.TryCompose(_graphId, container, type, arguments, out instance);
		}

		public bool TryDestroy(object instance)
		{
			return _composer.TryDestroy(instance);
		}
	}
}
